#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#include <iostream>
void create();
void run();
void clearscreen();
int menu();
int quit();
void createfood();
void drawh(int x, int y);
void drawb(int x, int y);
void youlose();
void level();
void howtoplay();
void highscore();
void kekhung();
int kt();
int ktlose();

struct point
{
	short x,y;
};
point s[500];
int sl,i,spd,score=0,oldscore=0,plus,t;
point food,newh;

void gotoxy(short x, short y) 
{
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void khungscore(int& score)
{
	gotoxy(52,4);
	printf("Score: ");
	printf("%d",score);
	
}
void youlose(int x)
{
	unsigned char c;
	clearscreen();
	gotoxy(33,6);
	printf("YOU LOSE!!");
	if(x>oldscore)
	{
		oldscore = x;
		gotoxy(33,11);
		printf("You have");
		gotoxy(28,12);
		printf(" a new high score:");
		gotoxy(36,13);
		printf("%d",oldscore);
	}
	else
	{
		gotoxy(29,8);
		printf(" Your score: %d",x);
	}
	gotoxy(28,18);
	printf("Press ESC to return");	
	do
	{
		if (kbhit())  
			c = getch();
		if (c==27) 
			{
				clearscreen();
				menu();
			}
	}
	while(c!=27);
	}
void level()
{
	int x=32,y=7;
	gotoxy(x,y);
	printf(" ___LEVEL___");
	gotoxy(x,y+=2);
	printf("Easy");
	gotoxy(x,++y);
	printf("Medium");
	gotoxy(x,++y);		
	printf("Hard");	
	gotoxy(x,y+=2);
	printf("Please choose...");
	x=45; y=9;	// toa do ki tu mui ten
	gotoxy(x,y);
	printf("%c",283);
	unsigned char c;
	do
	{
		if (kbhit())  // keyboard hit - code chay khi bam mot phim nao do
		{
			c = getch();
			
				if (c==224)  // nam trong nhom phim dieu khien 
				{
				gotoxy(x,y);
				printf(" ");
				c = getch();
				if(y == 9 && c == 72) 	// neu o dong playgame thi an len se chay xuong duoi cung
					{ y += 2; c=20;}
				else
					if(y == 11 && c == 80)	// neu o dong exit thi an xuong se chay len dau
						{y -= 2; c=20;}
					else
						if ((c==72) || (c==80))	// cac dong khac binh thuong
							 { c==72?--y:++y; c=20;}		
								
				gotoxy(x,y);	//di chuyen con tro den toan do x y tren man hinh
				printf("%c",283);
				}
			if ((c==13) && (y==9))  { spd=100; plus=1; } //do kho
			if ((c==13) && (y==10)) { spd=80; plus=2; }
			if ((c==13) && (y==11)) { spd=60; plus=3; }
			
		}
	}	
	while( c!=13);
	if(c==13)
	{
		clearscreen();
	}	
}
void howtoplay()
{
	gotoxy(28,7);
	printf("  ___HOW TO PLAY___")	;
	gotoxy(28,9);
	printf("Use up, left, right,");
	gotoxy(28,10);
	printf("down to move snake...");
	gotoxy(28,12);
	printf("Good luck!");
	gotoxy(28,18);
	printf("Press ESC to return");	
	unsigned char c;
	do
	{
		if (kbhit())  
			c = getch();
			if (c==27) 
			{
				clearscreen();
				menu();
			}
	}
	while(c!=27);
}
void highscore(int x)
{
	gotoxy(28,8);
	printf("  ___HIGH SCORE___");
	gotoxy(35,10);
	printf("%d",oldscore);
	gotoxy(28,18);
	printf("Press ESC to return");	
	unsigned char c;
	do
	{
		if (kbhit())  
			c = getch();
			if (c==27) 
			{
				clearscreen();
				menu();
			}
	}
		while(c!=27);
}

void clearscreen()// xoa tat ca chi chua lai khung
{
	// xoa trong khung
	int x,y;
	for(y = 3 ; y < 21 ; y++)
	{
		for(x = 26;x < 49;x++)	// toa do x = 33 -> 47
		{
			gotoxy(x,y);
			printf(" ");
		}
	}
	// Xoa khung score
	for(y=4;y<6;y++)
		for(x=52;x<70;x++)
		{
			gotoxy(x,y);
			printf(" ");
		}
}
void kekhung()
{
		for(int i=0;i<2;i++)// lui xuong 2 o
		printf("\n");
	for(int i=0;i<20;i++) // high = 20
	{
		for(int a=0;a<25;a++) // dich phai 25 khoang trang
			printf(" ");
		for(int j=0;j<25;j++)	// wide = 25
		{
			if((i==0) || (i==19) || (j==0) || (j==24))
				printf("%c",177);
			else 	
				printf(" ");
		}	
		printf("\n");
	}
}
int menu()
{

		int x=32,y=9;
		gotoxy(32,6);
		printf("Game Snake!");
		gotoxy(x,y);
		printf("Play game"); // (32,9)
		gotoxy(x,++y);	
		printf("How to play ");	//	(32,10)
		gotoxy(x,++y);
		printf("High score ");	//	(32,11)
		gotoxy(x,++y);
		printf("Exit ");	//	(32,12)
		gotoxy(x-5,y+=4);
		printf("Khoa Thien - Nhat Hai");
		gotoxy(x-1,++y);
		printf("GAME DESIGNER");
		
		x=45; y=9;	// toa do ki tu mui ten
		gotoxy(x,y);
		printf("%c",283);
		unsigned char c;
		do
		{
			if (kbhit())  // keyboard hit - code chay khi bam mot phim nao do
			{
				c = getch();
				if (c==224)  // nam trong nhom phim dieu khien 
				{
					gotoxy(x,y);
					printf(" ");
					c = getch();
					if(y == 9 && c == 72) 	// neu o dong playgame thi an len se chay xuong duoi cung
						y += 3;
					else
						if(y == 12 && c == 80)	// neu o dong exit thi an xuong se chay len dau
							y -= 3;
						else
							if ((c==72) || (c==80))	// cac dong khac binh thuong
								 c==72?--y:++y; 		
				}
					
					gotoxy(x,y);	//di chuyen con tro den toan do x y tren man hinh
					printf("%c",283);
					
			}
		}	
		while( c!=13);
		clearscreen();	// xoa man hinh
		if(y==9)
		{
			level();
			create();
			run();
		}
		if(y==10)
		{
			howtoplay();
		}
		if(y==11)
			highscore(score);
		if(y==12)
		{
			clearscreen();
			gotoxy(33,10);
			printf("Goodbye!");
			gotoxy(3,24);
			exit(0);
		}			
		return 1;
}

void drawh(int x, int y)// ve dau ran
{ 
	gotoxy(x,y);
	printf("%c",2);
}

void drawb(int x, int y)
{
	gotoxy(x,y);
	printf("%c",1);
}

void del(int x, int y)
{
	gotoxy(x,y);
	printf(" ");
}

void createfood()
{	short t,j;
	do
	{	t=0;
		srand(time(NULL));
		food.x=rand()%(48-26+1) + 26;
		food.y=rand()%(20-3+1) + 3;
		for(j=0;j<sl;j++) // sl la do dai con ran
	  		if (((s[j].x==food.x) && (s[j].y==food.y)) || ((newh.x==food.x) && (newh.y==food.y))) // neu thuc an trung voi toa do con ran thi random lai
			{
				t=1;
			  	break;
			}
	}
	while (t==1);
	
	gotoxy(food.x,food.y);		
	printf("%c",3);
}
void create()
{
	sl=3;
	s[0].x=rand()%(40-28+1) + 28;
	s[0].y=rand()%(18-5+1) + 5;
	s[1].x=s[0].x + 1;
	s[1].y=s[0].y;
	s[2].x=s[0].x + 2;
	s[2].y=s[0].y;			// con ran dau tien chay tu trai sang phai
	drawb(s[0].x,s[0].y); // create end
	drawb(s[1].x,s[1].y); // create body
	drawh(s[2].x,s[2].y); //create head
	createfood(); // khoi tao food ban dau
}
int kt() // kt da an food chua?
{
	if ((food.x==newh.x) && (food.y==newh.y)) return 1;
	return 0;
}

int ktlose()
{
	int tg=0,i;
	if ((newh.x<26) || (newh.x>48) || (newh.y<3) || (newh.y>20))
	{ 
	 	Beep(5000,1000); 
		youlose(score);
		return 1;
	}
	for(i=0;i<=sl-1;i++)
		if ((newh.x==s[i].x) && (newh.y==s[i].y)) 
		{ 
			Beep(5000,1000);
			youlose(score);
			return 1;
		}
	return 0;
}		
int quit()//kt khi bam esc
{  
	unsigned char key;
	gotoxy(60,6);
	printf("PAUSE!\n");
	gotoxy(50,7);
	printf("Are you sure you want to quit?\n");
	gotoxy(55,8);
	printf("Yes(Y) or No (N): ");
	do
	{
		gotoxy(73,8);
		key = getch();
		if(kbhit())
			key=getch();	
		if ((key=='y') || (key=='Y')) 
		{
			for(int y=6;y<9;y++)	
				for(int x=50;x<80;x++)
				{
					gotoxy(x,y);
					printf(" ");
				}
			return 1;
		}
		if ((key=='n') || (key=='N')) 
		{
			for(int y=6;y<9;y++)	
			for(int x=50;x<80;x++)
			{
				gotoxy(x,y);
				printf(" ");
			} 
			return 0;
		}
			
	}
	while ((key!='y') && (key!='Y') && (key!='n') && (key!='N')) ;
}	
void run()
{
	unsigned char c;
	short x=0,y=10;
	int Huong = 6;
	score = 0;
	khungscore(score);
	int k;
	do
	{
		//Xoa pause
		do
		{
			Sleep(spd);//toc do, do kho
			if (kbhit())
			{
				c = getch();
				if (c==224)
				{
					c = getch();
					if (c==72 && Huong!=8) Huong=2;//up
					if (c==75 && Huong!=6) Huong=4;//left
					if (c==77 && Huong!=4) Huong=6;//right
					if (c==80 && Huong!=2) Huong=8;//down
				}
			}
			
			if (Huong==2) {newh.y=s[sl-1].y-1; newh.x=s[sl-1].x;}   //thay doi toa do dau
			if (Huong==4) {newh.x=s[sl-1].x-1; newh.y=s[sl-1].y;}
			if (Huong==6) {newh.x=s[sl-1].x+1; newh.y=s[sl-1].y;}
			if (Huong==8) {newh.y=s[sl-1].y+1; newh.x=s[sl-1].x;}
			
			if ((ktlose()==1))  {youlose(score);t=1; ;break; /*c=27*/}  //bien t kiem soat tieng bip
			else
			{
				drawb(s[sl-1].x,s[sl-1].y);
				drawh(newh.x,newh.y);
				if (kt()==1 )
				{
					createfood();
					score+=plus;
					khungscore(score);
					Beep(1500,50);
					sl++;
				
				}
				else
				{
					del(s[0].x,s[0].y); //xoa diem cuoi
					for (i=0;i<sl-1;i++) //chuyen toa do len
					 	s[i]=s[i+1];
				}
					s[sl-1]=newh;
			}
		}
		while (c!=27); 	// khi chua bam esc
	 	c=20; // gan lai bien c bat ki
		k=quit();
	}while (k==0); // kt co tiep tuc khi bam esc (chon N)
		clearscreen();
		menu();
}

main()
{	
	srand(time(NULL));
	kekhung();
	menu();
	
} 
